<template>
	<div class="components-container">
        <div class="header-form">
          <div class="layui-inline">
              <label class="layui-form-label" >项目名称</label>
                  <div class="layui-input-inline">
              <el-select v-model="selectData.ChaidEquals">
                <el-option label="全部" :value="null"></el-option>
                <el-option v-for="(item,index) in Chaid" :key="index" :label="item.CHANAME" :value="item.CHAID" />
              </el-select>
            </div>

          </div>
            <div class="layui-inline">
                <label>处理日期从</label>
                <el-date-picker v-date v-model="selectData.ChddateGreaterAndEquals" placeholder="输入日期" @change="btnSearch"/>
                 到 <el-date-picker v-date v-model="selectData.ChddateLessAndEquals" type="date" placeholder="输入日期" @change="btnSearch"/>
            </div>
            <el-button type="primary" @click="btnSearch" :disabled="downloadLoading"  :loading="this.$store.state.isloading">搜索</el-button>
            <el-button :disabled="$store.state.isloading || downloadLoading"  @click="btnRepeat">重置</el-button>
        </div>
        <div style="margin-top:10px;">
          <ul class="toolList">
            <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)">
              <el-button type="primary" :disabled="$store.state.isloading || downloadLoading" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
            </li>
            <li>
            <el-button type="primary" :disabled="this.$store.state.isloading" @click="exportExcel">导出Excel</el-button>
            </li>
          </ul>
        </div>
        <printmodal ref="printPdf"></printmodal>
        <dataTable :headList="headList" :dataList="dataList" :pageOrder="pageOrder" @pageChange="pageChange" @selectRow="selectRow" :hiddenPageOrder="false" :height="height" :hiddenColumn="hiddenColumnInfo"></dataTable>
        <h1 style="text-align:right;margin-right:100px;color:red;font-size:20px;">{{message}}</h1>
        <!-- 详情 -->
        <sweet-modal ref="dialogInfo" class="dialog" :sweetModalWidth="sweetModalWidth" :sweetModalMaxWidth="sweetModalMaxWidth">
          <h3>{{ title }}</h3>
          <br/>
          <dataTable :headList="headListInfo" :dataList="dataListInfo" :pageOrder="pageOrderInfo"  @pageChange="pageChangeInfo" :hiddenColumn="false"></dataTable>
        </sweet-modal>
	</div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
.el-button + .el-button {
  margin-left: 0;
}
.btn {
  text-align: center;
}
</style>
<script>
import dataTable from "@/components/Table/DataTable";
import printmodal from "@/components/Pdf/Print";
import apiBase from "@/api/apiBase.js";
import { formatDate, timeStringToDate } from "@/utils/date.js";
import { ORDER } from "@/const/orderConst.js";
import { manage } from "@/mixins/managePage.js";
import { PRINT } from "@/components/Pdf/PDF.js";
import sweetModal from "@/components/sweetModal";

const EXPORT = "打印";
const INFO = "详情";
export default {
  name: "saleStatistics",
  mixins: [manage],
  data() {
    return {
      selectData: {
        ChaidEquals: null,
        ChddateGreaterAndEquals: new Date(),
        ChddateLessAndEquals: new Date(),
      },
      Chaid: [],
      downloadLoading:false,
      OutDataExcel:[],
      chargetype: [],
      title: null,
      order: null,
      message: null,
      height: "620",
      hiddenColumnInfo: true,
      //价格历史
      dataListInfo: [],
      headListInfo: [],
      //分页和排序
      pageOrderInfo: {
        orderType: null,
        orderSort: null,
        totalNum: null,
        pageSize: 10,
        currentPage: 1
      },
      sweetModalWidth: 80,
      sweetModalMaxWidth: 1000,
    
      chaTypes: [],
      chaTypeList: [],
    };
  },
  methods: {
    exportExcel() {
      var header
      var startTime;
          if(this.selectData.ChddateGreaterAndEquals){
            startTime = this.selectData.ChddateGreaterAndEquals.toLocaleDateString();
          }else{
            startTime = '空';
          }
          if(this.selectData.ChddateLessAndEquals != null){
            var endTime = this.selectData.ChddateLessAndEquals.toLocaleDateString();
            header = localStorage.getItem('orgname') + '殡仪服务汇总表' + '(' + startTime + '--' + endTime + ')';
          }else{
            header = localStorage.getItem('orgname') + '殡仪服务汇总表' + '(' + startTime + '--' + '至今' + ')';
          }
      if (this.dataList.length === 0){
          this.$message.error("无导出数据！");
          return;
      }
      this.downloadLoading = true;
      require.ensure([], () => {
        const { export_json_to_excel } = require("@/vendor/Export2Excel");
        export_json_to_excel(this.headList, this.OutDataExcel, header);
        this.downloadLoading = false;
      });
    },
    //功能操作
    operate(order) {
      this.order = order;
      this.dataRepeat();
      switch (this.order) {
        case INFO:
          this.title = "殡仪服务汇总表详情";
          this.dataInfo();
          break;
        case EXPORT:
          let Data = JSON.parse(JSON.stringify(this.selectData));
          this.$refs.printPdf.report(PRINT.PDFGENERALBUSSRET,this.getDataC());
          break;
        default:
      }
    },
    getcode(){
            let data = {
                SOURCE: '殡仪服务费用'
            };
            apiBase.DoService(
          data,
          apiBase.getApiAdress().Checkout.GetChargeBySource,
          res => {
            this.Chaid = res.data.content
          },
       )
    },
    getColumns() {
      let data = {};
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Column.GetCemBussDitRetColumns,
          res => {
            this.headList = res.data.content;
          }
        );
      
    },
    //获取收费项目
    getChaType() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Checkout.QueryChdByCyp,
        res => {
          let cdhTypeData = res.data.content;
          for (var i = 0; i < cdhTypeData.length; i++) {
            this.chaTypeList.push({
              value: cdhTypeData[i].CYPID,
              label: cdhTypeData[i].CYPNAME
            });
            for (var j = 0; j < cdhTypeData[i].list_Cha.length; j++) {
              this.chaTypes.push({
                value: cdhTypeData[i].list_Cha[j].CHAID,
                label: cdhTypeData[i].list_Cha[j].CHANAME
              });
            }
          }
        },
        res => {
          this.$message.error(res.data.msg);
        }
      );
    },
    getDataC(){
      let Data = JSON.parse(JSON.stringify(this.selectData));

          Data.ChddateGreaterAndEquals = formatDate(
            Data.ChddateGreaterAndEquals,
            "yyyyMMdd"
          );
          Data.ChddateLessAndEquals = formatDate(
            Data.ChddateLessAndEquals,
            "yyyyMMdd"
          );
      

      let data = {
        PageIndex: 1,
        PageSize: 200,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: Data
      };
      return data;
    },
    getData() {
      this.getColumns();    
      let Data = JSON.parse(JSON.stringify(this.selectData));    

          Data.ChddateGreaterAndEquals = formatDate(
            Data.ChddateGreaterAndEquals,
            "yyyyMMdd"
          );
          Data.ChddateLessAndEquals = formatDate(
            Data.ChddateLessAndEquals,
            "yyyyMMdd"
          );
      
      let data = {
        PageIndex: 1,
        PageSize: 200,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: Data
      };

        apiBase.DoService(
          data,
          apiBase.getApiAdress().Report.QueryFuneraServiceSummary,
          res => {
            this.hiddenColumnInfo = true;
            this.OutDataExcel = res.data.content.OutDataExcel;
            this.dataList = res.data.content.OutData;
            this.message = res.data.content.Message;
            this.pageOrder.totalNum = res.data.content.RecordCount;
          }
        );

    },
    //收费项目详情
    dataInfo() {
      var Data = {
        ChaidEquals: this.currentRow.CHLCHAID
      };
      let data = {
        PageIndex: this.pageOrderInfo.currentPage,
        PageSize: this.pageOrderInfo.pageSize,
        OrderBy: this.pageOrderInfo.orderType,
        OrderByType: this.pageOrderInfo.orderSort,
        Data: Data
      };
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Report.QuerySaleStatisticsDetail,
        res => {
          this.dataListInfo = res.data.content.OutData;
          this.pageOrderInfo.totalNum = res.data.content.RecordCount;
           this.$refs.dialogInfo.open();
        }
      );
    },
    //查询条件重置
    btnRepeat() {
      this.selectData = {
        ChddateGreaterAndEquals: new Date(),
        ChddateLessAndEquals: new Date(),
        ChaidEquals:null,
      };
        this.selectData.ChddateGreaterAndEquals = new Date();
        this.selectData.ChddateLessAndEquals = new Date();

      this.getData();
    },

    //分页和排序
    pageChangeInfo(value) {
      this.pageOrderInfo = value;
      // this.dataInfo();
    },
    btnShow(btn){
      switch (btn) {
        case EXPORT:
          return true;
          break;
        default: return false;
        }
    },
    //数据重置
    dataRepeat() {}
  },
  mounted() {
   
  },
  components: {
    dataTable,
    sweetModal,
    printmodal
  },
  //当前选中行
  selectRow(row) {
    this.currentRow = row;
    this.$emit("selectRow", row, "");
  },
  beforeCreate(){
      console.log(465469);
  },
  created() {
    this.getcode();
    this.getColumns();
    // this.getData();
    this.getChaType();
    this.headListInfo = [
      {
        ColumnAlign: "center",
        ColumnKey: "CHLMONEY",
        ColumnName: "单价",
        ColumnOrder: "CHLMONEY"
      },
      {
        ColumnAlign: "center",
        ColumnKey: "CHLNUM",
        ColumnName: "数量",
        ColumnOrder: "CHLNUM"
      },
      {
        ColumnAlign: "center",
        ColumnKey: "CHLMOUNT",
        ColumnName: "应收金额",
        ColumnOrder: "CHLMOUNT"
      },
      {
        ColumnAlign: "center",
        ColumnKey: "CHLREDUCTION",
        ColumnName: "减免金额",
        ColumnOrder: "CHLREDUCTION"
      },
      {
        ColumnAlign: "center",
        ColumnKey: "CHLDISCOUNT",
        ColumnName: "优惠金额",
        ColumnOrder: "CHLDISCOUNT"
      },
      {
        ColumnAlign: "center",
        ColumnKey: "CHLRECEIVED",
        ColumnName: "实收金额",
        ColumnOrder: "CHLRECEIVED"
      }
    ];
  }
};
</script>